package com.turing.selfwebservice.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * 时间处理类
 * 
 * @author 张小盼
 * 
 */
public class DateUtil {

	private static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
	private static final String DATE_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 使用构造函数获取webservice中使用XMLGregorianCalendar类型时间
	 * 
	 * @param java
	 *            .util.date
	 * @return XMLGregorianCalendar时间
	 * @throws Exception
	 */
	@SuppressWarnings("static-access")
	public static XMLGregorianCalendar getXMLGregorianCalendar(Date date)
			throws Exception {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		DatatypeFactory dtf = DatatypeFactory.newInstance();
		return dtf.newXMLGregorianCalendar(calendar.get(calendar.YEAR),
				calendar.get(calendar.MONTH) + 1, calendar
						.get(calendar.DAY_OF_MONTH), calendar
						.get(calendar.HOUR_OF_DAY), calendar
						.get(calendar.MINUTE), calendar.get(calendar.SECOND),
				calendar.get(calendar.MILLISECOND), calendar
						.get(calendar.ZONE_OFFSET)
						/ (1000 * 60));
	}

	/**
	 * 使用set赋值获取webservice中使用XMLGregorianCalendar类型时间，
	 * 
	 * @param date
	 *            年月日时分秒
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("static-access")
	public static XMLGregorianCalendar getXMLGregorianCalendarByDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(calendar.YEAR);
		int month = calendar.get(calendar.MONTH) + 1;
		int day = calendar.get(calendar.DAY_OF_MONTH);
		int hour = calendar.get(calendar.HOUR_OF_DAY);
		int minute = calendar.get(calendar.MINUTE);
		int second = calendar.get(calendar.SECOND);
		DatatypeFactory dtf = null;
		try {
			dtf = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
		XMLGregorianCalendar xmlgcl = dtf.newXMLGregorianCalendar();
		xmlgcl.setYear(year);
		xmlgcl.setMonth(month);
		xmlgcl.setDay(day);
		xmlgcl.setHour(hour);
		xmlgcl.setMinute(minute);
		xmlgcl.setSecond(second);
		return xmlgcl;
	}

	public static XMLGregorianCalendar getXMLGregorianCalendar(String dateStr)
			throws Exception {
		Date date = getDateYMDByString(dateStr);
		return getXMLGregorianCalendar(date);
	}

	/**
	 * 字符串转化为Date类型,时间转化错误返回null
	 * 
	 * @param time
	 * @return
	 */
	public static Date getDateByString(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMDHMS);
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 字符串转化为Date类型,时间转化错误返回null
	 * 
	 * @param time
	 * @return
	 */
	public static Date getDateYMDByString(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMD);
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * XMLGregorianCalendar时间类型转化为String类型
	 * 
	 * @param xmlgc
	 * @return
	 */
	public static String getStringByXMLGregorianCalendar(
			XMLGregorianCalendar xmlgc) {
		GregorianCalendar gcl = xmlgc.toGregorianCalendar();
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_YMDHMS);
		String date = formatter.format(gcl.getTime());
		return date;
	}

	/**
	 * 将标准时间转化为YYYY-MM-DD
	 * 
	 * @param date
	 * @return
	 */
	public static String getStringYMDByDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMD);
		String result = sdf.format(date);
		return result;
	}

	public static Date getDateFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMD);
		String result = sdf.format(date);
		Date d = null;
		try {
			d = sdf.parse(result);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
}
